﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Xml;
using AppShare.Data.Models;
using System.Collections;

namespace AppShare.Web
{
    public sealed partial class WikiData
    {
        private static WikiData instance;
        private IDictionary<String, WikiPage> dict;

        public static WikiData Current
        {
            get
            {
                if (instance == null)
                    instance = new WikiData();
                return instance;
            }
        }

        private WikiData()
        {
            var doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/wiki.xml"));

            dict = new Dictionary<String, WikiPage>();
            foreach (XmlNode node in doc.DocumentElement.SelectNodes("Page"))
            {
                dict.Add(node.Attributes["key"].Value, new WikiPage(node));
            }
        }

        public IDictionary<String, WikiPage> Pages
        {
            get
            {
                return dict;
            }
        }
    }

    public class WikiPage
    {
        public WikiPage(XmlNode node)
        {
            Title = node.Attributes["title"].Value;
            Header = node.SelectSingleNode("Header").InnerText;
            Footer = node.SelectSingleNode("Footer").InnerText;
            Author = node.SelectSingleNode("Author").InnerText;
            PublishDate = node.SelectSingleNode("PublishDate").InnerText;
            Contents = GetContents(node);
        }

        private IEnumerable GetContents(XmlNode node)
        {
            foreach (XmlNode p in node.SelectNodes("P"))
            {
                yield return new { P = p.InnerXml };
            }
        }

        public string Title { get; set; }
        public string Header { get; set; }
        public string Footer { get; set; }
        public string Author { get; set; }
        public string PublishDate { get; set; }
        public IEnumerable Contents { get; set; }
    }
}



